var form;
var $;
var table;
var layer;
var upload;
var node;
var layedit;
var user = JSON.parse(localStorage.getItem("user"));
var userId = user.userId;
var draftMicrocircle;
var index;
var artcleUrl;
var microcircleId;
layui.use(['form','jquery','table','layer','upload','layedit'],function() {
    form = layui.form;
    $ = layui.jquery;
    table = layui.table;
    layer = layui.layer;
    upload = layui.upload;
    layedit = layui.layedit;

    index = layedit.build("area",{
        height:300
    });

    //默认展示草稿
    showDraf(2);

    //选择文章分类
    ArticleClassification = function ArticleClassification() {
        layer.open({
            type:2,
            title:"文章分类",
            area:["500px","300px"],
            content:"/MicroCircle/article-classification.html",
        });
    }

    //上传图片
    upload.render({
        elem:"#test1",
        url:"/Upload/upload",
        method:"post",
        field:"file",
        accept:"file",
        done:function(res, index, upload){
            if (res.data != null) {
                artcleUrl = res.data.substring(res.data);
                $("input[name=artcleUrl]").val(artcleUrl);
                console.log(artcleUrl);
                layer.msg("上传成功");
            }else{
                layer.msg("上传失败");
            }
        }
    });
});

function showDraf(details){
    $.ajax({
        url:'/microcircle/getDraftMicrocircle',
        type: "post",
        dataType:'json',
        data: {"details": details},
        success:function (data) {
            draftMicrocircle = data.data;
            console.log(draftMicrocircle);
            if (data.data != null){
                $("input[name=artcleTitle]").val(data.data.artcleTitle);
                $("input[name=artcleCategroyName]").val(data.data.artcleCategroyName);
                $("input[name=acticleCategroyId]").val(data.data.acticleCategroyId);
                $("input[name=artcleUrl]").val(data.data.artcleUrl);
                $("input[name=microcircleId]").val(data.data.microcircleId);
                //为富文本编辑器赋值
                layedit.setContent(index, data.data.article, false);
            }else{
                $("input[name=artcleTitle]").val('');
                $("input[name=artcleCategroyName]").val('');
                $("input[name=acticleCategroyId]").val('');
                $("input[name=artcleUrl]").val('');
                $("input[name=microcircleId]").val('');
                layedit.setContent(index, '', false);
            }
        }
    });
}

//保存草稿
draft = function draft(){
    var artcleTitle = $("input[name=artcleTitle]").val();
    var acticleCategroyId = $("input[name=acticleCategroyId]").val();
    if (draftMicrocircle != null){
        var microcircleId = draftMicrocircle.microcircleId;
    }
    var article = layedit.getContent(index);
    var artcleUrl = $("input[name=artcleUrl]").val();
    var data = {
        userId: userId,
        artcleTitle: artcleTitle,
        acticleCategroyId: acticleCategroyId,
        microcircleId: microcircleId,
        article: article,
        artcleUrl: artcleUrl,
        updateId: userId,
    };
    //保存草稿（有草稿时修改）
    if (draftMicrocircle != null){
        console.log(data);
        $.ajax({
            url: "/microcircle/upDraftMicrocircle",
            type: "post",
            dataType: "json",
            data: data,
            success: function (data) {
                if (data.data){
                    showDraf(2);
                    addLog(3,2,'添加微圈');
                    layer.msg("保存成功");
                }
            }
        });
    }else{
        //无草稿时添加
        console.log(data);
        $.ajax({
            url: "/microcircle/addDraftMirList",
            type: "post",
            dataType: "json",
            data: data,
            success: function (data) {
                microcircleId = data.data.microcircleId;
                if (data.data){
                    showDraf(2);
                    addLog(3,2,'添加微圈');
                    layer.msg("保存成功");
                }
            }
        });
    }
}
//发布文章
release = function release() {
    var at = $("input[name=artcleTitle]").val();
    var an = $("input[name=artcleCategroyName]").val();
    var ac = layedit.getContent(index);
    if (at != '' && an != '' && ac != ''){
        console.log(11111);
        var artcleUrl = $("input[name=artcleUrl]").val();
        var microcircleId = $("input[name=microcircleId]").val();
        if (draftMicrocircle != null){
            var data = {
                productId: microcircleId,
                //microcircleImgUrl: draftMicrocircle.artcleUrl,
                microcircleImgUrl: artcleUrl,
                microcircleImgType: 3,
                createId: userId,
            }
            console.log(data);
            //添加微圈图片表
            $.ajax({
                url: "/microcircle/addMirImg",
                type: "post",
                dataType: "json",
                data: data,
                success: function (data) {
                    console.log(data.data);
                    if (data.data){
                        showDraf(2);
                        addLog(3,2,'添加微圈');
                        layer.msg("发布成功");
                    }
                }
            });
        }else{
            var artcleTitle = $("input[name=artcleTitle]").val();
            var acticleCategroyId = $("input[name=acticleCategroyId]").val();
            if (draftMicrocircle != null){
                var microcircleId = draftMicrocircle.microcircleId;
            }
            var article = layedit.getContent(index);
            var artcleUrl = $("input[name=artcleUrl]").val();
            var data = {
                userId: userId,
                artcleTitle: artcleTitle,
                acticleCategroyId: acticleCategroyId,
                microcircleId: microcircleId,
                article: article,
                artcleUrl: artcleUrl,
                updateId: userId,
            };
            $.ajax({
                url: "/microcircle/addMirList",
                type: "post",
                dataType: "json",
                data: data,
                success: function (data) {
                    console.log(data.data);
                    if (data.data){
                        showDraf(2);
                        addLog(3,2,'添加微圈');
                        layer.msg("发布成功");
                    }
                }
            });

        }
    }else{
        layer.msg("请输入内容");
    }

}

//添加操作日志
function addLog(logType,type,remark) {
    var user = JSON.parse(localStorage.getItem("user"));
    var userId = user.userId;
    $.ajax({
        url: "/microcircle/addLog",
        dataType: "json",
        type: "post",
        data: {"logType":logType,"type":type,"userId":userId,"remark":remark},
        success: function (data) {

        }
    });
}

//返回
function backOff() {
    var arr = localStorage.getItem("numArr");
    var numArr = arr.split(",");
    numArr.pop();
    window.parent.layui.element.tabChange('tab',numArr[numArr.length - 1]);
    localStorage.setItem("numArr",numArr);
}